package com.asj.house.controller;

import com.asj.house.client.HouseNoticeClient;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.utils.OSSUtil;
import com.asj.house.vo.param.NoticeParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 * 公告模块 前端控制器
 * </p>
 *
 * @author asj
 * @since 2021-02-24
 */
@RestController
@RequestMapping("/house-notice")
@Slf4j
@Api(value = "公告模块", tags = "公告模块")
public class HouseNoticeController {

    @Resource
    private HouseNoticeClient houseNoticeClient;

    @Resource
    private OSSUtil ossUtil;

    @GetMapping("/list")
    @ApiOperation(value = "公告列表")
    public RespEntity noticeList() {
        RespEntity respEntity = houseNoticeClient.noticeList();
        return respEntity;
    }

    @PostMapping("/insert")
    @ApiOperation(value = "发布公告")
    public RespEntity insertNotice(NoticeParam noticeParam) {
        RespEntity respEntity = houseNoticeClient.insertNotice(noticeParam);
        return respEntity;
    }

    /**
     * OSS文件上传
     * */
    @RequestMapping("/upload")
    @ApiOperation(value = "上传公告照片")
    public RespEntity upload(MultipartFile file){
        String fileName = ossUtil.getFileName("house/news/", file.getOriginalFilename());
        String url = ossUtil.getUrl(fileName);
        try {
            byte[] bytes = file.getBytes();
            ossUtil.upload(fileName, bytes);
            return new RespEntity(RespCode.SUCCESS, url);
        } catch (Exception e) {
            log.error("文件上传失败", e.getMessage());
            return new RespEntity(RespCode.UPLOAD_ERROR);
        }
    }
}
